// miniprogram/pages/book/addBook/addBook.js
const db = wx.cloud.database()
var filePath
var cloudPath
Page({

  /**
   * 页面的初始数据
   */
  uploadingImage() {
    wx.chooseImage({
      count: 1,
      success: res => {
        wx.showLoading({
          title: '更换中,请稍等',
        })
        filePath = res.tempFilePaths[0];
        cloudPath = "bookImage/" + new Date().getTime() + filePath.match(/\.[^.]+?$/)[0];
        this.setData({
          bookImage: filePath,
          change: true,
          fileName: cloudPath.substring(10)
        })
        wx.hideLoading();
      },
      fail: function(res) {
        wx.showToast({
          title: '选择图片失败!',
        })
      },
    })
  },

  edit(){
    getApp().sendRequest({
      url: "editBook",
      method: "GET",
      data: {
        id: this.data._id,
        bookname: this.data.bookname,
        bookimage: this.data.bookImage,
        "grade.id": this.data.grade_id
      },
      success: res => {
        if (res.data.code == 200) {
          wx.hideLoading();
          wx.showToast({
            title: "" + res.data.msg,
          })
          setTimeout(wx.navigateBack, 1500);
        } else {
          console.log("this.data.fileName", this.data.fileName)
          wx.cloud.deleteFile({
            fileList: [this.data.fileName],
          })
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail: res => {
        console.log("fail", res)
      },
      complete: res => {
        console.log("complete", res)
      }
    })
  },


  booknameInput(e) {
    this.data.bookname = e.detail.detail.value;
    this.setData({
      change: true
    })
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value,
      change: true
    })
    this.setData({
      grade_id: this.data.gradeLst[this.data.index].id
    })
  },
  handleClick() {
    wx.showLoading({
      title: '编辑中!',
    })
    wx.cloud.uploadFile({
      cloudPath,
      filePath,
      success: res2 => {
        if (this.data.bookImage) {
          wx.cloud.deleteFile({
            fileList : [this.data.bookImage],
          })
          this.setData({
            bookImage: res2.fileID
          })
        }
        this.edit()
      },
      fail: res4 => {
        wx.showToast({
          title: '上传失败',
        })
      },
      complete: res3 => {
        wx.hideLoading();
      }
    })

  },
  data: {
    index: 0,
    pic: "/images/default.jpg",
    gradeLst: [],
    gradeStrLst: [],
    book: null,
    change: false,
    fileName : ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载数据中',
    })
    getApp().sendRequest({
      url: "goAddBook",
      method: "GET",
      data: {
        bookId: options.bookid || -1
      },
      success: res => {
        if (res.data.code == 200) {
          for (var index in res.data.data.gradeLst) {
            this.data.gradeStrLst.push(res.data.data.gradeLst[index].gradename)
            if (res.data.data.gradeLst[index].gradename=="S1"){
              this.data.grade_id = res.data.data.gradeLst[index].id
            }
          }
          this.setData({
            gradeStrLst: this.data.gradeStrLst,
            gradeLst: res.data.data.gradeLst,
            grade_id: this.data.grade_id
          })
          if (res.data.data.book != null) {
            this.setData({
              bookname: res.data.data.book.bookname,
              bookImage: res.data.data.book.bookimage,
              _id: res.data.data.book.id,
              grade_id: res.data.data.book.grade.id,
              index: res.data.data.book.grade.id-1
            })
          }
        } else {
          wx.showToast({
            title: '加载超时,请稍后在试'
          })
        }
        wx.hideLoading();
      },
      fail: res => {
        console.log("fail", res)
      },
      complete: res => {
        console.log("complete", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})